<?php include('../../include/functions.inc.php'); ?>
<link href="../../bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
<script src="../../js/jquery.js" type="text/javascript"></script>
<script>jQuery.noConflict();</script>
<script src="../../bootstrap/js/bootstrap.js"></script>
<div id="table">
    <table width="100%" cellpadding="0" cellspacing="1" class="table table-striped table-hover table-bordered">
        <tbody>
            <tr>
                <th width="05%" rowspan="2" style="width:5%"><input type="checkbox" id="chkAll" name="chkAll" /></th>
                <th width="17%" rowspan="2">CARGO PENDIENTE</th>
                <th width="11%" rowspan="2">FECHA PAGO</th>
                <th width="14%" rowspan="2">PAGOS PREVIOS</th>
                <th width="07%" rowspan="2">MONTO</th>
                <th width="06%" rowspan="2">MORA</th>
                <th width="06%" rowspan="2">TOTAL</th>
                <th width="34%" colspan="2">CANCELAR</th>
            </tr>
            <tr>
                <th width="17%">Monto</th>
                <th width="17%">Porcentaje</th>
            </tr>
            <tr>
                <td colspan="6">&nbsp;</td>
                <td align="center"><input type="button" name="btnAplicarDescuentoGeneral" id="btnAplicarDescuentoGeneral" class="btn btn-info" value="Aplicar" title="Aplicar descuento a los cargos seleccionados"></td>
                <td align="center"><input type="text" name="txtAplicarDescuentoGeneralMonto" id="txtAplicarDescuentoGeneralMonto" value=""></td>
                <td align="center"><input type="text" name="txtAplicarDescuentoGeneralPorcentaje" id="txtAplicarDescuentoGeneralPorcentaje" value=""></td>
            </tr>  
            <?php
            $f = date('y-d-m');
            $r = $db->query_select("cllg_cargo", "DATE_PAYED IS NULL AND FK_ACCOUNT=" . getParam("ACCOUNT"), "DATE_LIMIT");
            $index = 0;

            foreach ($r as $c) {
                $now = date("y-m-d");
                $penal = 0;
                $total = $c['AMOUNT'];
                $parciales = "No";
                $moneda = $c["MONEDA"] == 1 ? "Q" : "$";
                if (strtotime($now) > strtotime($c['DATE_LIMIT'])) {
                    $penal = $c['PENALTY'];
                    $total+=$penal;
                     if( $total >= $c["TOTAL_PENDIENTE"] && $penal <= $c["TOTAL_PENDIENTE"] ) {
                         $penal = $c['PENALTY'] - ( $total - $c["TOTAL_PENDIENTE"] );
                     }
                }
                if (isset($c["TOTAL_PENDIENTE"]) && $c["TOTAL_PENDIENTE"] > 0) {
                    $total = $c['TOTAL_PENDIENTE'];
                    if( $penal < 0 ) {
                        $c['AMOUNT'] = $c['AMOUNT'] + $penal; 
                        $penal = 0;
                    }
                    $parciales = "Si";
                }
                $row = array("ID" => $c["ID"], "DESCRIPTION" => $c['DESCRIPTION'],
                    "AMOUNT" => number_format($c['AMOUNT'], 2, ".", ","),
                    "PENAL" => number_format($penal, 2, ".", ","),
                    "TOT" => $total);
                $index = $c["ID"];
                $json_string = json_encode($row);
                ?>
                <tr class="<?php fmod($index, 2) == 0 ? 'data line0' : 'data line1'; ?>" >
                    <td valign="middle">
                        <input type="checkbox" id="chk_<?php echo $index; ?>" name="chk_<?php echo $index; ?>" onclick='javascript:doit(this,<?php echo $json_string; ?>);' />
                        <input type="hidden" name="hdn_<?php print $index; ?>" id="hdn_<?php print $index; ?>" value="Y">
                    </td>
                    <td valign="middle"><?php echo $c['DESCRIPTION']; ?></td>
                    <td valign="middle"><?php echo $c['DATE_LIMIT']; ?></td>
                    <td valign="middle"><?php echo $parciales; ?></td>
                    <td valign="middle"><?php echo $moneda . " " . number_format($c['AMOUNT'], 2, ".", ","); ?></td>
                    <td valign="middle"><?php echo $moneda . " " . number_format($penal, 2, ".", ","); ?></td>
                    <td valign="middle"><?php echo $moneda . " " . number_format($total, 2, ".", ","); ?></td>
                    <td align="center" valign="middle">
                        <input type="text" id="mon_<?php echo $index; ?>" readonly="readonly" name="mon_<?php echo $index; ?>" 
                            value="<?php echo number_format($total, 2, ".", ","); ?>" onchange='javascript:setMonto(this,<?php echo $row["ID"]; ?>);' 
                            onkeyup="javascript:setMonto(this,<?php echo $row["ID"]; ?>);" />
                    </td>
                    <td align="center">
                        <input type="text" id="por_<?php echo $index; ?>" readonly="readonly" name="por_<?php echo $index; ?>" 
                            value="100" onchange='javascript:setPorc(this,<?php echo $row["ID"]; ?>);'
                            onkeyup="javascript:setPorc(this,<?php echo $row["ID"]; ?>);" />
                    </td>
                </tr> 
                <?php
            }
            ?>
        </tbody>
    </table>
    <script type="text/javascript" language="javascript">
        jQuery( function() {
            jQuery("#chkAll").click( function() {
                if( jQuery(this).is(':checked') ) {
                    jQuery("[id*='chk_']").each( function() {
                        if( !jQuery(this).is(':checked') ) {
                            id = jQuery(this).attr('id');
                            document.getElementById(id).click();    
                        }
                    });    
                }
                else {
                    jQuery("[id*='chk_']").each( function() {
                        if( jQuery(this).is(':checked') ) {
                            id = jQuery(this).attr('id');
                            document.getElementById(id).click();    
                        }
                    });
                }
            });
            jQuery("[id*='chk_']").click( function() {
                boolChecked = true;
                jQuery("[id*='chk_']").each( function() {
                    if( !jQuery(this).is(':checked') ) {
                        boolChecked = false;
                    }
                });
                jQuery("#chkAll").prop('checked', boolChecked);
            });
            jQuery("#txtAplicarDescuentoGeneralMonto").change( function(){
                jQuery("#txtAplicarDescuentoGeneralPorcentaje").val("");
            });
            jQuery("#txtAplicarDescuentoGeneralPorcentaje").change( function(){
                jQuery("#txtAplicarDescuentoGeneralMonto").val("");
            });
            jQuery("#btnAplicarDescuentoGeneral").click( function(){
                
                sinAplicarDescuentoGeneralMonto = jQuery("#txtAplicarDescuentoGeneralMonto").val();
                sinAplicarDescuentoGeneralPorcentaje = jQuery("#txtAplicarDescuentoGeneralPorcentaje").val();
                jQuery("[id*='chk_']").each( function() {
                    if( jQuery(this).is(':checked') ) {
                        id = jQuery(this).attr('id');
                        strNombre = jQuery(this).attr('id');
                        arrSplit = strNombre.split("_");
                        if( sinAplicarDescuentoGeneralMonto > 0 ) {
                            jQuery("#mon_"+arrSplit[1]).val(sinAplicarDescuentoGeneralMonto);    
                            setMonto(document.getElementById("mon_"+arrSplit[1]), arrSplit[1]);
                        }
                        if( sinAplicarDescuentoGeneralPorcentaje > 0 ) {
                            jQuery("#por_"+arrSplit[1]).val(sinAplicarDescuentoGeneralPorcentaje);
                            setPorc(document.getElementById("por_"+arrSplit[1]), arrSplit[1]);    
                        }
                    }
                });
                
            });
        });
    </script>
</div>